package com.examinantsdk.base.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.examinantsdk.R;
import com.scwang.smart.refresh.layout.api.RefreshHeader;
import com.scwang.smart.refresh.layout.api.RefreshKernel;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.constant.RefreshState;
import com.scwang.smart.refresh.layout.constant.SpinnerStyle;

public class CustomRefreshHeader extends LinearLayout implements RefreshHeader {
    private LayoutInflater inflater;
    private ImageView imageView;
    private boolean hasSetPullDownAnim = false;
    private int[] imge_ids = new int[]{
            R.mipmap.refresh_0,
            R.mipmap.refresh_1,
            R.mipmap.refresh_2,
            R.mipmap.refresh_3,
            R.mipmap.refresh_4,
            R.mipmap.refresh_5
    };
    private AnimationDrawable pullDownAnim;
    private AnimationDrawable refreshingAnim;

    public CustomRefreshHeader(Context context) {
        super(context);
        inflater = LayoutInflater.from(context);
        init(context);
    }

    public CustomRefreshHeader(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        inflater = LayoutInflater.from(context);
        init(context);
    }

    public CustomRefreshHeader(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public void init(Context context) {
        View header_view = View.inflate(context, R.layout.refresh_header_amin, this);
        imageView = header_view.findViewById(R.id.refresh_header_imge);

    }

    @NonNull
    @Override
    public View getView() {
        return this;
    }

    @NonNull
    @Override
    public SpinnerStyle getSpinnerStyle() {
        return SpinnerStyle.Translate;
    }

    @SuppressLint("RestrictedApi")
    @Override
    public void setPrimaryColors(@ColorInt int... colors) {

    }

    @SuppressLint("RestrictedApi")
    @Override
    public void onInitialized(@NonNull RefreshKernel kernel, int height, int maxDragHeight) {

    }
    @SuppressLint("RestrictedApi")
    @Override
    public void onMoving(boolean isDragging, float percent, int offset, int height, int maxDragHeight) {
        if (percent < 1) {
            int position = (int) (percent * imge_ids.length);
            imageView.setImageResource(imge_ids[position]);
            // imageView.setScaleX(percent);
            // imageView.setScaleY(percent);
            if (hasSetPullDownAnim) {
                hasSetPullDownAnim = false;
            }
        } else {
            imageView.setImageResource(imge_ids[imge_ids.length - 1]);
        }
        if (percent >= 1.0) {
            if (!hasSetPullDownAnim) {
                imageView.setImageResource(R.drawable.anim_refresh_header);
                pullDownAnim = (AnimationDrawable) imageView.getDrawable();
                // pullDownAnim.start();
                hasSetPullDownAnim = true;
            }
        }
    }
    @SuppressLint("RestrictedApi")
    @Override
    public void onReleased(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {

    }
    @SuppressLint("RestrictedApi")
    @Override
    public void onStartAnimator(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {

    }
    @SuppressLint("RestrictedApi")
    @Override
    public int onFinish(@NonNull RefreshLayout refreshLayout, boolean success) {
        if (pullDownAnim != null && pullDownAnim.isRunning()) {
            pullDownAnim.stop();
        }
        if (refreshingAnim != null && refreshingAnim.isRunning()) {
            refreshingAnim.stop();
        }
        hasSetPullDownAnim = false;
        return 0;
    }
    @SuppressLint("RestrictedApi")
    @Override
    public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {

    }

    @Override
    public boolean isSupportHorizontalDrag() {
        return false;
    }
    @SuppressLint("RestrictedApi")
    @Override
    public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState) {
        switch (newState) {
            case PullDownToRefresh:
                imageView.setImageResource(R.mipmap.refresh_0);
                break;
            case Refreshing:
                imageView.setImageResource(R.drawable.anim_refresh_header);
                refreshingAnim = (AnimationDrawable) imageView.getDrawable();
                refreshingAnim.start();
                break;
            case ReleaseToRefresh:
                break;
        }
    }
}
